package com.lams.service.system.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.lams.jdbc.IBaseDAO;
import com.lams.jdbc.QueryUtils;
import com.lams.service.BaseFacade;
import com.lams.service.system.IModuleFacade;
import com.lams.util.ResultEntity;
import com.lams.util.TreeEntity;

public class ModuleFacade extends BaseFacade implements IModuleFacade {
	private String queryAllMF;
	private String queryAllMFByRid;
	
	@Override
	public List<Map> findModule(List<Integer> rid) {
		return null;
	}
	public List<TreeEntity> findModuleByMR(List<Integer> rid, Integer mid){
		return null;
	}
	@Override
	public List<TreeEntity> getMenu(List<Integer> rid) {
		//取得所有的根模块
		//List<Map> rModules = md.findAllRootModule();
		Map params = new HashMap();
		params.put("ROLE_ID", rid.get(0));
		String sql = QueryUtils.getSqlBySqlCode(queryAllMFByRid, params);
		List list = QueryUtils.executeQuery(sql);
		List<TreeEntity> result = getRoot(list);
		return result;
	}
	public List<TreeEntity> getAllMF(String rid){
		Map params = new HashMap();
		params.put("ROLE_ID", rid);
		String sql = QueryUtils.getSqlBySqlCode(queryAllMF, params);
		List list = QueryUtils.executeQuery(sql);
		List<TreeEntity> result = getRoot(list);
		return result;
	}
	private List<TreeEntity> getRoot(List list){
		List<TreeEntity> result = new ArrayList<TreeEntity>();
		for(int i=0;i<list.size();i++){
			Map item = (Map) list.get(i);
			String pid = String.valueOf(item.get("P_ID"));
			String mid = String.valueOf(item.get("M_ID"));
			if("0".equals(pid)){
				TreeEntity node = new TreeEntity();
				node.setId(String.valueOf(mid));
				node.setChecked(true);
				node.setExpanded(true);
				node.setText(String.valueOf(item.get("NAME")));
				node.setChildren(getChildren(mid, list));
				result.add(node);
			}else{
				break;
			}
		}
		return result;
	}
	private List<TreeEntity> getChildren(String pmid,List list){
		List<TreeEntity> result = new ArrayList<TreeEntity>();
		for(int i=0;i<list.size();i++){
			Map item = (Map) list.get(i);
			String pid = String.valueOf(item.get("P_ID"));
			String fid =  String.valueOf(item.get("ID"));
			String mid = String.valueOf(item.get("M_ID"));
			String flag = String.valueOf(item.get("FLAG"));
			if("0".equals(fid) && pid.equals(pmid)){
				TreeEntity node = new TreeEntity();
				node.setId(mid);
				node.setChecked(true);
				node.setExpanded(true);
				node.setText(String.valueOf(item.get("NAME")));
				node.setChildren(getChildren(mid, list));
				result.add(node);
			}else if(!"0".equals(fid) && mid.equals(pmid)){
				TreeEntity node = new TreeEntity();
				node.setId(fid);
				node.setChecked("0".equals(flag)?false:true);
				node.setClazz(String.valueOf(item.get("CLAZZ")));
				node.setLeaf(true);
				node.setUrl(String.valueOf(item.get("HREF")));
				node.setText(String.valueOf(item.get("NAME")));
				node.setClassName(String.valueOf(item.get("COMPETENCE")));
				//node.setChildren(getChildren(mid, list));
				result.add(node);
			}else{
				continue;
			}
		}
		return result;
	}
	@Override
	public void newModule(Map m) {
		// TODO Auto-generated method stub

	}

	@Override
	public ResultEntity findSingleModule(Integer id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void modifyModule(Map m) {
		// TODO Auto-generated method stub

	}

	@Override
	public List<Map> operateModule() {
		
		return null;
	}

	@Override
	public void removeModule(Map m) {
		// TODO Auto-generated method stub

	}
	
	public String getQueryAllMF() {
		return queryAllMF;
	}

	public void setQueryAllMF(String queryAllMF) {
		this.queryAllMF = queryAllMF;
	}

	public String getQueryAllMFByRid() {
		return queryAllMFByRid;
	}

	public void setQueryAllMFByRid(String queryAllMFByRid) {
		this.queryAllMFByRid = queryAllMFByRid;
	}
	
	public static void main(String[] args) {
		ModuleFacade mf = new ModuleFacade();
		mf.setQueryAllMF("QUERYALLMF");
		List<TreeEntity> list = mf.getAllMF("-1");
		ResultEntity result = new ResultEntity();
		result.setData(list);
		System.out.println(ResultEntity.decode(result));
	}
}
